//
//  SWStageSummaryHeader.swift
//  ChildStory
//
//  Created by linhan on 2016/12/16.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit

fileprivate let ContentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15)
fileprivate let TextContentInsets:UIEdgeInsets = UIEdgeInsetsMake(43, 14, 14, 14)
fileprivate let ContentFont:UIFont = UIFont.systemFont(ofSize: 14)

class SWStageSummaryHeader: UITableViewCell {
    weak var summary:SWStageSummary?
    
    private var _change:Bool = true
    private var _cellWidth:CGFloat = 0
    
    private var _contentView:UIView = UIView()
    private var _contentLabel:UILabel = UILabel()
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        _cellWidth = cellWidth
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func getCellHeight(summary:SWStageSummary, cellWidth:CGFloat) -> CGFloat
    {
        let content:String = summary.title ?? ""
        let contentWidth:CGFloat = cellWidth - ContentInsets.left - ContentInsets.right - TextContentInsets.left - TextContentInsets.right
        let textHeight:CGFloat = StringUtil.getStringHeight(content, font: ContentFont, width: contentWidth)
        return TextContentInsets.top + textHeight + TextContentInsets.bottom
    }
    
    private func setup()
    {
        selectionStyle = .none
        backgroundColor = UIColor.clear
        contentView.backgroundColor = UIColor.clear
        
        let contentWidth:CGFloat = _cellWidth - ContentInsets.left - ContentInsets.right
        _contentView.frame = CGRectMake(ContentInsets.left, 0, contentWidth, 0)
        _contentView.backgroundColor = UIColor.white
        contentView.addSubview(_contentView)
        
        let titleLabel:UILabel = UILabel()
        titleLabel.text = "阶段总览"
        titleLabel.textColor = UIColor(hex: 0x585757)
        titleLabel.font = UIFont.systemFont(ofSize: 17)
        titleLabel.sizeToFit()
        titleLabel.frame = CGRectMake(TextContentInsets.left, 15, titleLabel.width, titleLabel.height)
        _contentView.addSubview(titleLabel)
        
        let textContentWidth:CGFloat = _cellWidth - ContentInsets.left - ContentInsets.right - TextContentInsets.left - TextContentInsets.right
        _contentLabel.font = ContentFont
        _contentLabel.textColor = UIColor(hex: 0x6c6c6c)
        _contentLabel.numberOfLines = 0
        _contentLabel.frame = CGRectMake(TextContentInsets.left, TextContentInsets.top, textContentWidth, 0)
        _contentView.addSubview(_contentLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let summary = summary, _change
        {
            _change = false
            
            let content:String = summary.title ?? ""
            let textHeight:CGFloat = StringUtil.getStringHeight(content, font: ContentFont, width: _contentLabel.width)
            _contentLabel.text = content
            _contentLabel.height = textHeight
            
            _contentView.height = _contentLabel.bottom + TextContentInsets.bottom
        }
    }
    
}
